package com.hengfei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hengfei.dao.CheckGroupDao;
import com.hengfei.dao.TCheckgroupCheckitemMapper;
import com.hengfei.dao.TCheckgroupMapper;
import com.hengfei.entity.PageResult;
import com.hengfei.entity.QueryPageBean;
import com.hengfei.pojo.CheckGroup;
import com.hengfei.pojo.TCheckgroup;
import com.hengfei.pojo.TCheckgroupCheckitemExample;
import com.hengfei.pojo.TCheckgroupCheckitemKey;
import com.hengfei.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    //这个是逆向工程生成的mapper
    @Autowired
    private TCheckgroupMapper tCheckgroupMapper;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;

    //新增数据
    @Override
    public void add(TCheckgroup checkGroup, Integer[] checkitemIds) {
        //添加checkgroup表
        tCheckgroupMapper.insertSelective(checkGroup);

        //添加中间关系表
        createMappingInCheckgroupAndCheckitem(checkGroup.getId(), checkitemIds);

    }

    //条件查找
    @Override
    public PageResult findPageConditional(QueryPageBean queryPageBean) {
        //PageHelper pageHelper = new PageHelper();
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据检查组的id，查询检查项的id
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return tCheckgroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public TCheckgroup findById(Integer id) {

        return tCheckgroupMapper.selectByPrimaryKey(id);
    }

    //修改检查组信息
    @Override
    public void edit(TCheckgroup checkGroup, Integer[] checkitemIds) {
        //修改 TCheckgroup 表的信息
        tCheckgroupMapper.updateByPrimaryKeySelective(checkGroup);
        //删除原本的中间表信息
        deleteCheckgroupAndCheckitemMappingByCheckgroupId(checkGroup.getId());
        //建立新的连接
        createMappingInCheckgroupAndCheckitem(checkGroup.getId(), checkitemIds);
    }

    //根据checkGroupId清除中间表信息
    @Override
    public void deleteCheckgroupAndCheckitemMappingByCheckgroupId(Integer id) {
        TCheckgroupCheckitemExample example = new TCheckgroupCheckitemExample();
        example.createCriteria().andCheckgroupIdEqualTo(id);
        tCheckgroupCheckitemMapper.deleteByExample(example);
    }

    //建立checkgroup和item项的连接
    @Override
    public void createMappingInCheckgroupAndCheckitem(Integer checkGroupId, Integer[] checkitemIds) {
        for (Integer checkitemId : checkitemIds) {
            tCheckgroupCheckitemMapper.insertSelective(new TCheckgroupCheckitemKey(checkGroupId, checkitemId));
        }
    }

    //根据ID删除检查组
    @Override
    public void deleteById(Integer id) {
        //先清除中间表
        deleteCheckgroupAndCheckitemMappingByCheckgroupId(id);
        //再清除checkgroup表的该条记录
        tCheckgroupMapper.deleteByPrimaryKey(id);
    }

    //查询所有检查组
    @Override
    public List<TCheckgroup> findAllCheckGroup() {
        return tCheckgroupMapper.selectByExample(null);
    }


}
